package com.community.sqlapp.controller;

import com.community.sqlapp.service.FileInfoService;
import com.community.sqlapp.entity.FileInfoPo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/fileconfig")
public class FileUploadMethodController {

    @Autowired
    private FileInfoService fileInfoService;

    /**
     * 显示文件上传页面
     */
    @GetMapping("/upload")
    public String showUploadPage() {
        return "fileupload";
    }

    /**
     * 文件上传，入库文件ID、文件名称,千问的qwen-long模型存储doc、docx时调这个接口
     * @param file
     * @return
     */
    @PostMapping("/fileupload")
    @ResponseBody
    public Map<String, Object> qwenLongFileUpload(@RequestParam("file") MultipartFile file) {
        Map<String, Object> response = new HashMap<>();
        try {
            // 调用文件上传服务
            FileInfoPo fileInfo = fileInfoService.uploadFile(file);

            response.put("success", true);
            response.put("message", "文件上传成功");
            response.put("fileId", fileInfo.getFileId());
            response.put("fileName", fileInfo.getFileName());
            response.put("id", fileInfo.getId());
        } catch (Exception e) {
            response.put("success", false);
            response.put("error", e.getMessage());
        }
        return response;
    }

    /**
     * 文件下载接口
     * @param file 文件名
     * @return
     */
    @GetMapping("/download")
    public ResponseEntity<byte[]> downloadFile(@RequestParam("file") String file) throws IOException {
        File targetFile = new File(file);
        if (!targetFile.exists()) {
            return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
        }
        byte[] fileBytes = Files.readAllBytes(targetFile.toPath());
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
        headers.setContentDisposition(ContentDisposition.attachment().filename(targetFile.getName()).build());
        return new ResponseEntity<>(fileBytes, headers, HttpStatus.OK);
    }

    /**
     * 获取文件信息
     * @param file
     * @return
     * @throws Exception
     */
    @PostMapping("/fileInfo")
    @ResponseBody
    public Map<String, Object> parseFileContent(@RequestParam("file") MultipartFile file) throws Exception {
        Map<String, Object> response = new HashMap<>();
        try {
            String fileName = file.getOriginalFilename();
            FileInfoPo fileInfo = fileInfoService.getFileInfoByFileName(fileName);

            if (fileInfo != null) {
                response.put("success", true);
                response.put("fileInfo", fileInfo);
            } else {
                response.put("success", false);
                response.put("message", "文件信息未找到");
            }
        } catch (Exception e) {
            response.put("success", false);
            response.put("error", e.getMessage());
        }
        return response;
    }
}